#!/bin/sh
set -e

#DEBHELPER#

KEY_DIR="/etc/biometric-auth/key"
KEY_FILE="${KEY_DIR}/community-multidevice-aes.key"
KEY_LEN=32

if [ ! -f "/etc/biometric-auth/" ]
then
	mkdir -p /etc/biometric-auth/
	touch /etc/biometric-auth/biometric-drivers.conf
fi

if [ ! -f ${KEY_FILE} ]
then
	mkdir -p ${KEY_DIR}
	touch ${KEY_FILE}
	AES_KEY=`dd if=/dev/urandom bs=${KEY_LEN} count=1 2>/dev/null | base64 | head -c ${KEY_LEN}`
	echo -n ${AES_KEY} > ${KEY_FILE}
	chmod 0600 ${KEY_FILE}
fi

DRIVER_LIST="upekts uru4000 aes4000 aes2501 upektc aes1610 fdu2000 vcom5s \
	upeksonly vfs101 vfs301 aes2550 upeke2 aes1660 aes2660 aes3500  \
	upektc_img etes603 vfs5011 vfs0050 elan"

case "${1}" in
	configure)
		for driver in ${DRIVER_LIST}; do
			biometric-config-tool add-driver -f ${driver} \
				/usr/lib/biometric-authentication/drivers/${driver}.so
			biometric-config-tool set-key -i ${driver} AESKey ${KEY_FILE}
		done
		;;

	abort-upgrade|abort-remove|abort-deconfigure)
		;;

	*)
		echo "postinst called with unknown argument \`${1}'" >&2
		exit 1
		;;
esac

exit 0
